<?php
require('includes/top.php');

$serial_no= $_POST['serial_no'];
$laptop_id= $_POST['laptop_id'];


	
if($_POST['type']=="Available")
{
			$sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptop_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptop_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '1',`laptop_condition`=".$old." WHERE laptop_id = '".$laptop_id."'";
			$result=mysql_query($sql);
			}
}

elseif($_POST['type']=="Onhold")
{
			$sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptop_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptop_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '2',`laptop_condition`=".$old." WHERE laptop_id = '".$laptop_id."'";
			$result=mysql_query($sql);
	        }		
}
elseif($_POST['type']=="DamagedU")
{
			$sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptop_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptop_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '3',`laptop_condition`=".$old." WHERE laptop_id = '".$laptop_id."'";
			$result=mysql_query($sql);
}
}
elseif($_POST['type']=="DamagedO")
{
			$sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptop_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptop_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '4',`laptop_condition`=".$old." WHERE laptop_id = '".$laptop_id."'";
			$result=mysql_query($sql);
            }
}			
elseif($_POST['type']=="Substitute")
{
			$sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptop_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptop_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '1',`laptop_condition`=".$old.",`laptop_type`='S' WHERE laptop_id = '".$laptop_id."'";
			$result=mysql_query($sql);
			}
}


//Laptop Acc

if(isset($_POST["acc_id"]) && !empty($_POST["acc_id"])){	
					  $Acc_id=array_values($_POST["acc_id"]);
					  $LapAcctype=array_values($_POST["LapAcctype"]);					  								
	 				  $count=sizeof($Acc_id);										  
					  for($i=0;$i<=$count;$i++){					  						  					  
					     if(trim($Acc_id[$i])!=''){
						    $AccId=addslashes(trim($Acc_id[$i]));
							$LapAccType=addslashes(trim($LapAcctype[$i]));											
								
							if($LapAccType=="Available")
									{
					$sql="INSERT INTO used_laptop_acc(`acc_id`,`laptop_id`) VALUES ('$AccId','$laptop_id')";
					$result=mysql_query($sql);
									}																														
																				
						 }										
					  
		}
					
	  }

//Acc
if(isset($_POST["accessories_id"]) && !empty($_POST["accessories_id"])){	
					  $Accessories_id=array_values($_POST["accessories_id"]);
					  $Acctype=array_values($_POST["Acctype"]);
					  $card_no=array_values($_POST["card_no"]);
					  $company=array_values($_POST["company"]);
					  $depot_location=array_values($_POST["depot_location"]);
					  $accessories_name=array_values($_POST["accessories_name"]);					  								
	 				  $count=sizeof($Accessories_id);					  
					  for($i=0;$i<=$count;$i++){					  						  					  
					     if(trim($Accessories_id[$i])!=''){
						    $AccessoriesId=addslashes(trim($Accessories_id[$i]));
							$AccType=addslashes(trim($Acctype[$i]));
							$CardNo=addslashes(trim($card_no[$i]));
							$Company=addslashes(trim($company[$i]));
							$DepotLocation=addslashes(trim($depot_location[$i]));
							$AccessoriesName=addslashes(trim($accessories_name[$i]));	
							if($AccType=="Available")
									{					
									if($AccessoriesName!="Data card")
									{
			$sql="INSERT INTO used_accessories_data(`accessoriesdata_id`,`accessories_name`,`laptop_id`) VALUES ('$AccessoriesId','$AccessoriesName','$laptop_id')";
			$result=mysql_query($sql);
			
									}
									else
									{
			$sql="INSERT INTO used_accessories_data(`accessoriesdata_id`,`accessories_name`,`company_name`,`card_no`,`laptop_id`) VALUES ('$AccessoriesId','$AccessoriesName','$Company','$CardNo','$laptop_id')";
			$result=mysql_query($sql);
									}
			
			$query1=mysql_query("Select * from accessories_data where accessories_name='".$AccessoriesName."'");
		$rows=mysql_fetch_array($query1);	
		$AccId = $rows['accessoriesdata_id'];
		
		$query=mysql_query("Select * from accessories_stock where accessories_id = '".$AccId."' and depot_location='".$DepotLocation."'");
			while($queryrow=mysql_fetch_array($query))
			{
			$quantity=$queryrow['quantity'] + 1;
			
			$sql2="UPDATE accessories_stock SET `quantity` = '$quantity' WHERE accessories_id = '".$AccId."' and depot_location='".$DepotLocation."'";
			$result2=mysql_query($sql2);	
			}				
			if($AccessoriesName=="Data card")
			{
			$query2=mysql_query("Select * from data_card where card_no='".$CardNo."'");
			$rows=mysql_fetch_array($query2);		
			$sql5="INSERT INTO used_datacard(`datacard_id`,`card_no`,`laptop_id`) VALUES ('".$rows['datacard_id']."','$CardNo','$laptop_id')";
			$result5=mysql_query($sql5);
				
			$sql4="UPDATE data_card SET `status` = '0' WHERE datacard_id = '".$rows['datacard_id']."'";
			$result4=mysql_query($sql4); 
			}

		}																														
																				
 }									
					  
		}
					
	  }
	  //substitute laptop here
	  $serialsub_no=$_POST['serials_no'];
	  $laptopsub_id=$_POST['laptops_id'];
	  echo $_POST['typesub'];
	  if($_POST['typesub']=='Available')
	  {
	  echo "sdd";
	        $sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			 while($result3 = mysql_fetch_array($sql3)){
			   $newboxno=$result3['boxes']+1;
			   $sql4="UPDATE laptop_details SET `box_no` = '".trim($newboxno)."' WHERE `laptop_id`  = '".trim($laptopsub_id)."'";
			   $result4=mysql_query($sql4);			  
			  }			
			$sql21=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '". $laptopsub_id."'");
            while($result21= mysql_fetch_array($sql21)){
			$old=$result21['laptop_condition'] + 1;
			$sql="UPDATE laptop_details SET status = '1',laptop_condition='".$old."' WHERE laptop_id = '".$laptopsub_id."'";
			$result=mysql_query($sql);
			} 

	  }
	  elseif($_POST['typesub']=='Onhold')
	  {
	  
	     $sql3=mysql_query("Select max(box_no) as boxes from laptop_details ");
		   while($result3=mysql_fetch_array($sql3)){
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET box_no = '".$newboxno."' WHERE laptop_id = '". $laptopsub_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '". $laptopsub_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			$sql="UPDATE laptop_details SET status = '2',laptop_condition='".$old."'WHERE laptop_id = '".$laptopsub_id."'";
			$result=mysql_query($sql);
			}

	  }
	  elseif($_POST['typesub']=='DamagedU')
	  {
	    $sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
			while($result3 = mysql_fetch_array($sql3)){
			
			$newboxno = $result3['boxes']+1;		
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptopsub_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptopsub_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '3',`laptop_condition`=".$old." WHERE laptop_id = '".$laptopsub_id."'";
			$result=mysql_query($sql);
			
           }
	  }
	  else
	  {
	    $sql3=mysql_query("Select max(box_no) as boxes from laptop_details");
		   while($result3 = mysql_fetch_array($sql3)){
			$newboxno = $result3['boxes']+1;
			$sql4="UPDATE laptop_details SET `box_no` = '".$newboxno."' WHERE laptop_id = '".$laptopsub_id."'";
			$result4=mysql_query($sql4);
			}			
			$sql2=mysql_query("Select laptop_id,laptop_condition,serial_no,box_no from laptop_details WHERE laptop_id = '".$laptopsub_id."'");

			while($result2 = mysql_fetch_array($sql2)){
			$old=$result2['laptop_condition'] + 1;
			
			$sql="UPDATE laptop_details SET `status` = '4',`laptop_condition`=".$old." WHERE laptop_id = '".$laptopsub_id."'";
			$result=mysql_query($sql);
			
            }
	  }
	  
	  	 
if($result>0)
{
header('Location:laptop_status_tech.php?v=success');
}
else
{
header('Location:laptop_status_tech.php?v=err');
}
?>